﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.model;
using l1cs.server.templates;

namespace l1cs.server.serverpackets
{
    public class S_PrivateShop : ServerBasePacket
    {
        public S_PrivateShop( L1PcInstance pc, int objectId, int type )
        {
            L1PcInstance shopPc = (L1PcInstance)L1World.getInstance().findObject( objectId );

            if ( shopPc == null )
            {
                return;
            }

            writeC( Opcodes.S_OPCODE_PRIVATESHOPLIST );
            writeC( type );
            writeD( objectId );

            if ( type == 0 )
            {
                List<L1PrivateShopSellList> list = shopPc.getSellList();
                int size = list.Count;
                pc.setPartnersPrivateShopItemCount( size );
                writeH( size );
                for ( int i = 0; i < size; i++ )
                {
                    L1PrivateShopSellList pssl = list[i];
                    int itemObjectId = pssl.getItemObjectId();
                    int count = pssl.getSellTotalCount() - pssl.getSellCount();
                    int price = pssl.getSellPrice();
                    L1ItemInstance item = shopPc.getInventory().getItem( itemObjectId );
                    if ( item != null )
                    {
                        writeC( i );
                        writeC( item.getBless() );
                        writeH( item.getItem().getGfxId() );
                        writeD( count );
                        writeD( price );
                        writeS( item.getNumberedViewName( count ) );
                        writeC( 0 );
                    }
                }
            }
            else if ( type == 1 )
            {
                List<L1PrivateShopBuyList> list = shopPc.getBuyList();
                int size = list.Count;
                writeH( size );
                for ( int i = 0; i < size; i++ )
                {
                    L1PrivateShopBuyList psbl = list[i];
                    int itemObjectId = psbl.getItemObjectId();
                    int count = psbl.getBuyTotalCount();
                    int price = psbl.getBuyPrice();
                    L1ItemInstance item = shopPc.getInventory().getItem( itemObjectId );
                    foreach ( L1ItemInstance pcItem in pc.getInventory().getItems() )
                    {
                        if ( item.getItemId() == pcItem.getItemId()
                          && item.getEnchantLevel() == pcItem.getEnchantLevel()
                        )
                        {
                            writeC( i );
                            writeD( pcItem.getId() );
                            writeD( count );
                            writeD( price );
                        }
                    }
                }
            }
        }

        override
        public byte[] getContent()
        {
            return getBytes();
        }

        override public String getType()
        {
            return "[S] S_PrivateShop";
        }
    }
}